#!/data/Software/mydan/perl/bin/perl -I/data/Software/mydan/CI/lib -I/data/Software/mydan/CI/private/lib
use strict;
use warnings;

use Logs;
use FindBin qw( $RealBin );
use Temp;
use MIME::Base64;
use File::Basename;
use LWP::UserAgent;
use Encode;
use Util;
use JSON;



$| ++;

=head1 SYNOPSIS

    db => $mysql,
    logs => 日志对象

    #index uuid temp path addr ticketid tags

=cut

return sub
{
    my %param = @_;

    my ( $db,$logs, $index, $uuid, $temp, $path, $addr, $ticketid, $tags, $treeid  ) 
        = @param{qw(db logs index uuid temp path addr ticketid tags treeid )};

    $logs = Logs->new( 'code.build.dump.rely.openc3fileserver' ) unless $logs;

    my $temppath = "$RealBin/../logs/$temp/$uuid/rely/$index";
    if( -d $temppath )
    {
        $logs->die( "clean old temppath fail:$!" ) if system "rm -rf '$temppath'";
    }
    $logs->die( "mkdir temppath fail:$!" ) if system "mkdir -p '$temppath'";

    my $ua = LWP::UserAgent->new();
    $ua->agent('Mozilla/9 [en] (Centos; Linux)');
    $ua->default_header( Util::envinfo( qw( appkey appname ) ) );
    $ua->timeout( 10 );
    my $res = $ua->get( "http://api.job.open-c3.org/fileserver/$treeid" );
    my $cont = $res->content;
    $logs->die( "calljob fail: $cont" ) unless $res->is_success;

    my $data = eval{JSON::from_json $cont};
    $logs->die( "calljob fail: $cont : no json $@" ) if $@;

    $logs->die( "calljob fail: $cont : stat fail" ) unless $data->{stat};

    my $tagdata;
    if( $tags )
    {
        map{ $tagdata = $_ if $_->{name} eq $tags }@{$data->{data}};
    }
    else
    {
        $data = pop @{$data->{data}};
    }

    $logs->die( "nofind version" ) unless $tagdata;

    my $version = $tagdata->{name};
    my $filepath = "/data/glusterfs/fileserver/$treeid/$tagdata->{md5}";

    $logs->die( "clone error:$!" ) if system "cd '$temppath' && rm -rf data info && mkdir data && mkdir info && tar -zxvf '$filepath' -C data && echo '$version' > info/version";

    if( $path )
    {
        $logs->die( "path format error" ) unless $path =~ /^[a-zA-Z0-9\.\-\_\/]+$/ && $path =~ /[a-zA-Z0-9]+/;
        my $subpath = "$RealBin/../logs/$temp/$uuid/data/$path";
        my $pathbase = dirname $subpath;

        unless( -d $pathbase )
        {
            $logs->die( "mkdir subpath fail:$!" ) if system( "mkdir -p '$pathbase'" );
        }

        if( -e $subpath )
        {
            $logs->die( "remove subpath fail:$!" ) if system( "rm -rf '$subpath'" );
        }

        $logs->die( "mv subpath fail:$!" ) if system "mv '$temppath/data' '$subpath'";
    }
    else
    {
        my $subpath = "$RealBin/../logs/$temp/$uuid/data$index";
        my $pathbase = dirname $subpath;

        unless( -d $pathbase )
        {
            $logs->die( "mkdir subpath fail:$!" ) if system( "mkdir -p '$pathbase'" );
        }

        if( -e $subpath )
        {
            $logs->die( "remove subpath fail:$!" ) if system( "rm -rf '$subpath'" );
        }

        $logs->die( "mv subpath fail:$!" ) if system "mv '$temppath/data' '$subpath'";
    }
}
